package com.nit.lab.service.Impl;

import com.nit.lab.dao.LoginDao;
import com.nit.lab.dao.UserDao;
import com.nit.lab.dto.DtoLoginUser;
import com.nit.lab.dto.DtoUser;
import com.nit.lab.dto.DtoUser4WebLoginBack;
import com.nit.lab.dto.utils.DtoAjaxMsg;
import com.nit.lab.service.LoginService;
import com.nit.lab.utils.HttpServletUtil;
import com.nit.lab.utils.SystemConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by CYT on 2017/11/19.
 */
@Service
public class LoginServicelmpl implements LoginService {
    @Resource
    private LoginDao loginDao;
    @Resource
    private HttpServletUtil servletUtil;

    @Override/*@Override是伪代码,表示重写*/
    public boolean findUserName(String userName) throws Exception {
        return loginDao.findUserName(userName);
    }

    @Override
    public boolean findUserPhone(String userPhone) throws Exception {
        return loginDao.findUserPhone(userPhone);
    }

    @Override
    public boolean findUserEmail(String userEmail) throws Exception {
        return loginDao.findUserEmail(userEmail);
    }

    @Override
    public DtoAjaxMsg selectUser(String userName, String userCode) throws Exception {
//         DtoLoginUser dtoLoginUser = loginDao.selectUser(userName,userCode);
        DtoAjaxMsg jsonDto = new DtoAjaxMsg();
        try {
            List<DtoLoginUser> listUser = loginDao.selectUser(userName, userCode);
            if (listUser == null) {
                jsonDto.setAjaxInfo(SystemConstants.Failure);
                jsonDto.setAjaxObject(SystemConstants.ErrorTips);
            } else if (listUser.size() == 0) {
                jsonDto.setAjaxInfo(SystemConstants.Failure);
                jsonDto.setAjaxObject(SystemConstants.FailureTips_name);
            } else {
                DtoLoginUser user = (DtoLoginUser) listUser.get(0);
                HttpServletRequest request = servletUtil.getRequest();
                HttpSession sessionUser = request.getSession();
                sessionUser.setAttribute(SystemConstants.SessionName, user);
                jsonDto.setAjaxInfo(SystemConstants.Success);
//                DtoUser4WebLoginBack user4WebLoginBack = new DtoUser4WebLoginBack(user.getUserId(), user.getActorId());
                jsonDto.setAjaxObject(user);
            }
        } catch (Exception e) {
            System.out.println("selectUser_error");
        }
        return jsonDto;
    }

    @Override
    public boolean changCode(String userEmail, String userPhone) throws Exception {
        return loginDao.changCode(userEmail, userPhone);
    }

    @Override
    @Transactional
    public Integer insertUserInfo(DtoLoginUser dtoLoginUser) throws Exception {
        Integer result = SystemConstants.Insert_Default;
        //Insert_Default = -1
        try {
            result = this.loginDao.insertUserInfo(dtoLoginUser.toBEAN());
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return result;
    }
}
